<?php

abstract class Kernel_Form_Element_Filter_Factory 
{

    /**
     * Return filter by given definition
     *
     * @param   mixed   $definition
     * @return  Kernel_Form_Element_Filter_Interface
     */
    static public function create($definition) 
    {
        switch (gettype($definition)) 
        {
            // in case of string - this is filter name
            case 'string':
                $name = $definition;
                $params = array();
                break;

            // in case of array: first param - filter name, others - filter params
            case 'array':
                $name = array_shift($definition);
                $params = (array)array_shift($definition);
                break;

            // other types are not recognized
            default:
                trigger_error('Unsuppoted filter definition type', E_USER_ERROR);
                break;
        }
        $class = self::_getClassName($name);
        return new $class($params);
    }

    /**
     * Возвращает наименование класса.
     *
     * @param   string  $name
     * @return  string
     */
    static private function _getClassName($name) 
    {
        return false === strpos($name, '_')
            ? 'Kernel_Form_Element_Filter_' . str_camelize($name) : $name;
    }
    
}
